package zhao.ben.quan.controller;

import com.aliyuncs.exceptions.ClientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import zhao.ben.quan.constant.MessageConstant;
import zhao.ben.quan.constant.RedisConstant;
import zhao.ben.quan.constant.RedisMessageConstant;
import zhao.ben.quan.entity.Result;
import zhao.ben.quan.utils.SMSUtils;
import zhao.ben.quan.utils.ValidateCodeUtils;

@RestController//@RequestBody+@Controller  标注这是一个selver层和返回字符串格式数据
@RequestMapping("/validateCode")//映射地址

public class ValidateCodeController {

    @Autowired//注入redis连接池对象
    private JedisPool jedisPool;

    //用户在线体检预约的发送验证码
    @RequestMapping("/send4Order")
    public Result send4Order(String telephone){
        //随机生成4位数的验证码
        Integer integer = ValidateCodeUtils.generateValidateCode(4);
        try {
            //发送成功
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE ,telephone,integer.toString());
            //将验证码保存到redis
            Jedis resource = jedisPool.getResource();
            //时间五分钟
            resource.setex(telephone+ RedisMessageConstant.SENDTYPE_ORDER,300,integer.toString());
            return new Result(true,MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);//验证码发送失败
        }
    }


    //用户在线体检预约的发送验证码
    @RequestMapping("/send4Login")
    public Result send4Login(String telephone){
        //随机生成4位数的验证码
        Integer integer = ValidateCodeUtils.generateValidateCode(6);
        try {
            //发送成功
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE ,telephone,integer.toString());
            //将验证码保存到redis
            Jedis resource = jedisPool.getResource();
            //时间五分钟
            resource.setex(telephone+ RedisMessageConstant.SENDTYPE_LOGIN,300,integer.toString());
            return new Result(true,MessageConstant.SEND_VALIDATECODE_SUCCESS);//验证码发送成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);//验证码发送失败
        }
    }
}
